<?php
namespace Admin\Controller;
use Think\Controller;
class ShopAccountController  extends IndexController {


    public function lists(){
        $this->display();
    }


    public function get_money_list(){
        $manager=D("Manager");
        $ManagerMoneyLog=D("ManagerMoneyLog");
        $res=$manager->where("username='".$_SESSION['name']."'")->find();
        if(!$res){
            $where='';
        }else{
            $where='manager_id='.$res['id'];
        }


        $result=$ManagerMoneyLog->where($where)->order("create_time desc")->field("money,s_money,action, commit_user,create_time as bulid_time,update_time")->select();

        foreach($result as $key=>&$value){
            $value['bulid_time']=date("Y/m/d/H:i:s",$value['bulid_time']);
            if($value['update_time']!="" &&$value['update_time']!=null && $value['action']==2 && $value['commit_user']=="admin"){
                $value['update_time']=1; //标识已经给商户打款了
            }
            //减去汇率的信息
            $value['true_money']= $value['money']-$value['s_money'];
        }

        if(!$result){
            echo json_encode(0); exit;
        }
        echo json_encode($result); exit;
    }


    public function get_money(){
                $manager=D("Manager");
                $res=$manager->where("username='".$_SESSION['name']."'")->field("money,cash,shop_id")->find();
                if($res!=0){
                    $Shopbase=D("ShopBase");
                    $rr=$Shopbase->where("id=".$res['shop_id'])->find();
                    $res['bz']=$rr['bz'];
                }else{
                    $res['bz']=0;
                }

                if(!$res){
                    echo json_encode(0); exit;
                }
                echo json_encode($res); exit;
    }
    //提现
    public function confirm($money){
        if($_SESSION['name']=='admin'){
            echo json_encode(0); exit;
        }
        if(intval($money)<5000){
            echo json_encode(2);exit;
        }
        $manager=D("Manager");
        $manager_money_log=D("ManagerMoneyLog");
        $res=$manager->where("username='".$_SESSION['name']."'")->field("id,username,money,cash,shop_id")->find(); 
        if(!$res){
            echo json_encode(0); exit;
        }
        if(intval($res['money'])<$money){
            echo json_encode(3);exit;
        }
        
        $shopBase=D("shopBase");
        $r1=$shopBase->where('id='.$res['shop_id'])->find();
        $data['money']=$res['money']-$money;
        $data['cash']=$res['cash']+$money-$r1['u_poundage']*$money;
        $manager->startTrans(); 
        $ress=$manager->where("username='".$_SESSION['name']."'")->save($data);
        if(!$ress){
            echo json_encode(0); 
            $manager->rollback();
        }
        
         $opt['manager_id']=$res['id'];
         $opt['money']=$money;
         $opt['s_money']=$r1['u_poundage']*$money;
         $opt['action']=1;
         $opt['create_time']=time();
         $shi=$opt['money']-$opt['s_money'];
         $opt['log']="名为".$res['username']."的商户于".date("Y-m-d H:i:s",$opt['create_time'])."执行了提现操作,提现金额为".$money."手续费".$opt['s_money'].",到账金额为".$shi;
         $r=$manager_money_log->add($opt);
         if(!$r){
            echo json_encode(0); 
            $manager->rollback();
         }
         $manager->commit();
         echo json_encode(1); exit;
    }

}